<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BCafeVoucherCooker
 *
 * @author jianfeng
 */
class BCafeVoucherCooker extends BAVoucherCooker {

    //put your code here

    public function onShowAddApplyPermitChoose() {
        $this->updateControl->addRenderView('voucherType_applyPermit_add_choose');
    }

    public function onShowAddApplyPermit() {

        $permitTypeData = $this->postManager->populatePostDataByIndex($this->postData, 'choose_permitType');

        $permitCooker = new BAPermitCooker();
        $permitType = NULL;
        $permit = NULL;
        if ($permitTypeData != NULL) {
            $permitType = reset($permitTypeData);
            //$permitType = $permitCooker->getPermitType($permitTypeName);
        }

        if ($permitType != NULL) {
            $permitCooker->setCurrentPermitType($permitType);
        } else {
            $permit = $permitCooker->getcurrentPermit();

            if ($permit != NULL) {
                $permitType = $permitCooker->getPermitTypeByPermit($permit)->getName();
            }
        }

        $theModel = new BDynamicFormModel();

        $permitField = 'media_permit_addVoucher_permit';
        $roleIdField = 'media_roleId_addVoucher_permit';
        $expireTimeField = 'media_expireTime_addVoucher_permit';

        $theModel->addField($permitField);
        $theModel->addField($roleIdField);
        $theModel->addField($expireTimeField);

        if ($permit != NULL) {
            if ($permit->getItem('permit') != NULL) {
                $theModel->$permitField = $permit->getItem('permit')->getValue();
            }
            if ($permit->getItem('roleId') != NULL) {
                $theModel->$roleIdField = $permit->getItem('roleId')->getValue();
            }
            if ($permit->getItem('expireTime') != NULL) {
                $theModel->$expireTimeField = $permit->getItem('expireTime')->getValue();
            }
        }

        $viewData = array(
            'model' => $theModel,
            'permitField' => $permitField,
            'roleIdField' => $roleIdField,
            'expireTimeField' => $expireTimeField
        );

        $view = 'voucherType_applyPermit_add_rolePermit';

        if ($permitType == BAPermitCooker::PermitType_birthPermit) {
            $birthTimeSpanField = 'media_birthTimeSpan_addVoucher_permit';
            $timeUnitField = 'media_timeUnit_addVoucher_permit';
            $theModel->addField($birthTimeSpanField);
            $theModel->addField($timeUnitField);
            $viewData['birthTimeSpanField'] = $birthTimeSpanField;
            $viewData['timeUnitField'] = $timeUnitField;
            $view = 'voucherType_applyPermit_add_birthPermit';
            if ($permit != NULL) {
                if ($permit->getItem('birthTimeSpan') != NULL) {
                    $theModel->$birthTimeSpanField = $permit->getItem('birthTimeSpan')->getValue();
                }
                if ($permit->getItem('timeUnit') != NULL) {
                    $theModel->$timeUnitField = $permit->getItem('timeUnit')->getValue();
                }
            }
        }

        if ($permitType == BCafePermitCooker::PermitType_PermitByBillTimeInterval) {
            $billTimeIntervalField = 'media_billTimeInterval_addVoucher_permit';
            $timeUnitField = 'media_timeUnit_addVoucher_permit';
            $theModel->addField($billTimeIntervalField);
            $theModel->addField($timeUnitField);
            $viewData['billTimeIntervalField'] = $billTimeIntervalField;
            $viewData['timeUnitField'] = $timeUnitField;
            $view = 'voucherType_applyPermit_add_billIntervalPermit';
            if ($permit != NULL) {
                if ($permit->getItem('billTimeInterval') != NULL) {
                    $theModel->$billTimeIntervalField = $permit->getItem('billTimeInterval')->getValue();
                }
                if ($permit->getItem('timeUnit') != NULL) {
                    $theModel->$timeUnitField = $permit->getItem('timeUnit')->getValue();
                }
            }
        }

        $this->updateControl->addRenderView('applyPermitAdd', $view, $viewData);
    }

    /*
      public function getExpireTime($voucher = NULL) {

      if ($voucher == NULL) {
      $voucher = $this->getCurrentVoucher();
      } else {
      $voucher = $this->getVoucher($voucher);
      }

      $timeUnit = NULL;
      $timeLength = NULL;

      if ($voucher instanceof BTreeList) {
      $voucherType = $this->getVoucherTypeByVoucher($voucher);
      if ($voucherType != NULL) {
      $timeUnit = $this->getVoucherTypeAttribute('validTimeUnit', $voucherType);
      $timeLength = $this->getVoucherTypeAttribute('validTimeLength', $voucherType);
      }

      if ($timeLength != NULL && $timeUnit != NULL) {
      if ($timeLength > 1) {
      $timeUnit .= 's';
      }
      }

      $userInfoId = $voucher->getItem('userInfoId')->getValue();
      $userInfoCooker = new BAUserCooker();
      $userInfo = $userInfoCooker->getUserInfo($userInfoId);

      $permitId = $voucher->getItem('permitId')->getValue();
      $permitCooker = new BAPermitCooker();
      $permit = $permitCooker->getPermit($permitId);
      $voucherAddTime = $voucher->getItem('addTime')->getValue();

      //计算生日代金券的过期时间,以生日为计算点，可用是1个月过期，或者指定的
      //天数和星期数过期
      if ($permitCooker->isMemberOfPermitType($permit, BAPermitCooker::PermitType_birthPermit)) {
      if ($userInfo->getItem('birth') != NULL) {
      $birth = $userInfo->getItem('birth')->getValue();
      if ($timeLength != NULL && $timeUnit != NULL) {
      $expireTime = strtotime('+' . $timeLength . ' ' . $timeUnit, strtotime($birth));
      if ($expireTime > strtotime('+10 days', $voucherAddTime)) {
      return $expireTime;
      } else {//领取后给
      return strtotime('+10 days', $voucherAddTime);
      }
      }
      }
      }

      //计算客户回访激励代金券的过期时间，将在用户领取日开始计算，加上指定的时间
      if ($permitCooker->isMemberOfPermitType($permit, BCafePermitCooker::PermitType_PermitByBillTimeInterval)) {
      if ($userInfo->getItem('birth') != NULL) {
      $birth = $userInfo->getItem('birth')->getValue();
      if ($timeLength != NULL && $timeUnit != NULL) {
      $expireTime = strtotime('+' . $timeLength . ' ' . $timeUnit, strtotime($birth));
      return $expireTime;
      }
      }
      }
      }
      }
     * 
     */
}
